package com.example.myapplication.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.myapplication.entity.Message;
import com.example.myapplication.utils.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class MessageDao {

    private DBHelper dpHelper;

    public MessageDao(Context context) {
        dpHelper = new DBHelper((context));
    }

    // 添加一条消息
    public void saveMessage(Message message) {
        String sql = "insert into message(id, userName, latestMessage, time, portrait) values(?,?,?,?,?)";
        dpHelper.getWritableDatabase().execSQL(sql, new Object[]{null, message.getUserName(),
                message.getLatestMessage(),
                message.getTime(),
                message.getPortrait()});
        Log.i("MessageDao", "execSQL: " + sql);
    }

    // 根据ID删除message
    public void deleteMessageById(int id) {
        String sql = "delete from message where id = ?";
        dpHelper.getWritableDatabase().execSQL(sql, new Object[]{id});
        Log.i("MessageDao", "execSQL: " + sql);
    }

    // 更新message
    public void update(Message message) {
        String sql = "update message set userName = ?, latestMessage = ?,time = ?, portrait = ? where id = ?";
        dpHelper.getWritableDatabase().execSQL(sql, new Object[]{message.getUserName(), message.getLatestMessage(),
            message.getTime(), message.getPortrait(), message.getId()});
        Log.i("MessageDao", "execSQL: " + sql);
    }

    // 查询所有Message
    public List<Message> findAllMessages() {
        List<Message> messageList = new ArrayList<>();
        String sql = "select id, userName, latestMessage, time, portrait from message";
        Cursor cursor = dpHelper.getReadableDatabase().rawQuery(sql, null);
        Log.i("MessageDao", "rawQuery: " + sql);
        while(cursor.moveToNext()) {
            Message message = new Message();
            message.setId(cursor.getInt(cursor.getColumnIndex("id")));
            message.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
            message.setLatestMessage(cursor.getString(cursor.getColumnIndex("latestMessage")));
            message.setTime(cursor.getString(cursor.getColumnIndex("time")));
            message.setPortrait(cursor.getString(cursor.getColumnIndex("portrait")));
            messageList.add(message);
        }
        return messageList;
    }

    // 根据userName查询message
    public Message findMessageByUserName(String userName) {
        String sql = "select id, userName, latestMessage, time, portrait from message where userName = ?";
        Cursor cursor = dpHelper.getReadableDatabase().rawQuery(sql, new String[]{userName});
        Log.i("MessageDao", "rawQuery: " + sql);
        while(cursor.moveToNext()) {
            Message message = new Message();
            message.setId(cursor.getInt(cursor.getColumnIndex("id")));
            message.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
            message.setLatestMessage(cursor.getString(cursor.getColumnIndex("latestMessage")));
            message.setTime(cursor.getString(cursor.getColumnIndex("time")));
            message.setPortrait(cursor.getString(cursor.getColumnIndex("portrait")));
            return message;
        }
        return null;
    }



}
